package com.habdqn.studentmanager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.habdqn.studentmanager.bean.Classes;
import com.habdqn.studentmanager.bean.Teacher;
import com.habdqn.studentmanager.constant.ClassType;
import com.habdqn.studentmanager.constant.LogType;
import com.habdqn.studentmanager.constant.TeacherType;
import com.habdqn.studentmanager.exception.StudentException;
import com.habdqn.studentmanager.mapper.ClassesMapper;
import com.habdqn.studentmanager.mapper.TeacherMapper;
import com.habdqn.studentmanager.service.ClassesService;
import com.habdqn.studentmanager.util.SystemLogger;

/**
 * @author Administrator
 * 
 *         2017年5月18日
 * 
 */
@Service
public class ClassesServiceImpl implements ClassesService {

	private static Logger logger = LoggerFactory.getLogger(ClassesServiceImpl.class);

	@Resource
	private ClassesMapper classesMapper;

	@Resource
	private TeacherMapper teacherMapper;

	@Resource
	private SystemLogger systemLogger;

	@Override
	public void add(Classes bean) {
		
		try {
			Integer status = ClassType.notOpen.toInteger();
			
			// 如果毕业时间为空，设置为结课状态
			if (null != bean.getEndDate()) {
				status = ClassType.close.toInteger();
			}
			else if (null == bean.getPreparatoryDate() && 
				null == bean.getOpenDate() &&
				null == bean.getTrainDate() &&
				null == bean.getEndDate()) {
				// 如果时间都为空，设置为未开课状态
				status = ClassType.notOpen.toInteger();
			}
			else {
				// 其他情况为在读状态
				status = ClassType.open.toInteger();
			}
			
			// 设置状态
			bean.setStatus(status);
			classesMapper.insert(bean);
		}
		catch (DuplicateKeyException e) {
			logger.error("重复的班级名称: {}", bean.getName(), e);
			throw new StudentException("班级已存在", e);
		}
		catch (RuntimeException e) {
			logger.error("添加班级失败", e);
			throw new StudentException("添加班级失败", e);
		}
		
		systemLogger.record(LogType.AddType, "新增班级:" + bean.getName());
	}

	@Override
	public void update(Classes bean) {
		try {
			
			Classes db = classesMapper.find(bean);
			Integer status = db.getStatus();
			
			// 如果班级状态不是结课状态
			if (null != status && !ClassType.close.equals(status)) {
				if (null != bean.getEndDate()) {
					status = ClassType.close.toInteger();
				}
				else if (null == bean.getPreparatoryDate() && 
					null == bean.getOpenDate() &&
					null == bean.getTrainDate() &&
					null == bean.getEndDate()) {
					// 如果时间都为空，设置为未开课状态
					status = ClassType.notOpen.toInteger();
				}
				else {
					// 其他情况为在读状态
					status = ClassType.open.toInteger();
				}
			}
			
			// 设置状态
			bean.setStatus(status);
			classesMapper.update(bean);
		}
		catch (RuntimeException e) {
			logger.error("更新班级失败", e);
			throw new StudentException("更新班级失败", e);
		}
		systemLogger.record(LogType.UpdateType, "修改班级信息，班级名称：" + bean.getName());
	}

	@Override
	public void delete(Classes bean) {
		// 没有删除需求
	}

	@Override
	public PageInfo<Classes> findList(Classes bean, Integer pageNum, Integer pageSize) {

		List<Classes> lists = null;

		try {
			if (null != pageNum && null != pageSize) {
				PageHelper.startPage(pageNum, pageSize);				
			}
			lists = classesMapper.findList(bean);
		}
		catch (RuntimeException e) {
			logger.error("查询班级列表失败", e);
			throw new StudentException("查询班级列表失败", e);
		}

		return new PageInfo<Classes>(lists);
	}

	@Override
	public Classes find(Classes bean) {
		try {
			bean = classesMapper.find(bean);
			if (null == bean) {
			    throw new RuntimeException();
			}
			List<Teacher> teachers = teacherMapper.findListByClass(bean, TeacherType.TeacherType.toInteger());
			List<Teacher> instructors = teacherMapper.findListByClass(bean, TeacherType.InstructorType.toInteger());
			bean.setTeachers(teachers);
			bean.setInstructors(instructors);
		}
		catch (RuntimeException e) {
			logger.error("查询班级失败", e);
			throw new StudentException("该班级不存在", e);
		}
		return bean;
	}
	
	@Override
	public int addClassesTeacher(Classes bean) {
		int i = -1;
		
		try {
			i = classesMapper.insertCT(bean);
		}
		catch (DuplicateKeyException e) {
			logger.error("该信息已存在", e);
			throw new StudentException("该信息已存在", e);
		}
		catch (RuntimeException e) {
			logger.error("添加失败", e);
			throw new StudentException("添加失败", e);
		}
		systemLogger.record(LogType.AddType, "添加教师:" + bean.getExt2() + "与班级:" + bean.getName() + "的关联关系");
		return i;
	}

	@Override
	public int delClassesTeacher(Classes bean) {
		int i = -1;
		try {
			i = classesMapper.deleteCT(bean);
		}
		catch (RuntimeException e) {
			logger.error("删除失败", e);
			throw new StudentException("删除失败", e);
		}
		
		if (0 == i) {
			throw new StudentException("没有该条记录");
		}
		 
		systemLogger.record(LogType.DelType, "删除教师:" + bean.getExt2() + "与班级:" + bean.getName() + "的关联关系");
		return i;
	}

	@Override
	public int updateClassesTeacher(Classes bean) {
		throw new StudentException("没实现该功能");
	}
}
